using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VVDPOS
    {
        public DataRow Convert2VDPOS(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryVDPOS(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["VDPMANDANT"] = " ".PadRight(2 ); 
         dwReturn["VDPAUFTYP"] = " ".PadRight(1 ); 
         dwReturn["VDPAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["VDPAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["VDPAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["VDPPOS"] = " ".PadRight(2 ); 
         dwReturn["VDPLO"] = " ".PadRight(2 ); 
         dwReturn["VDPVERSART"] = " ".PadRight(3 ); 
         dwReturn["VDPSPED"] = " ".PadRight(3 ); 
         dwReturn["VDPVERSDAT"] = " ".PadRight(0 ); 
         dwReturn["VDPAVIS"] = " ".PadRight(8 ); 
         dwReturn["VDPRANG"] = " ".PadRight(2 ); 
         dwReturn["VDPVERSADR"] = " ".PadRight(8 ); 
         dwReturn["VDPPLZ"] = " ".PadRight(12 ); 
         dwReturn["VDPORT"] = " ".PadRight(40 ); 
         dwReturn["VDPAUFM"] = " ".PadRight(2 ); 
         dwReturn["VDPMG"] = " ".PadRight(0 ); 
         dwReturn["VDPFILLER"] = " ".PadRight(36 ); 
         dwReturn["VDPAENZ"] = " ".PadRight(0 ); 
         dwReturn["VDPAENB"] = " ".PadRight(8 ); 
         dwReturn["VDPAEND"] = " ".PadRight(0 ); 
         dwReturn["VDPNEUB"] = " ".PadRight(8 ); 
         dwReturn["VDPNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapVDPOS(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveVDPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryVDPOS(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapVDPOS(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertVDPOS(dwLocal, ITXDBVERSION);
            }
            UpdateVDPOS(dwLocal);
        }
        public void InsertVDPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO VDPOS(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryVDPOS(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM VDPOS WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM VDPOS  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "VDPOS";
            Return = Result;
            return Return;
        }
        public void DeleteVDPOS(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM VDPOS   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateVDPOS(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE VDPOS SET "
                    +"VDPMANDANT= :VDPMANDANT,"
                    +"VDPAUFTYP= :VDPAUFTYP,"
                    +"VDPAUFKRS= :VDPAUFKRS,"
                    +"VDPAUFLFD= :VDPAUFLFD,"
                    +"VDPAUFPOS= :VDPAUFPOS,"
                    +"VDPPOS= :VDPPOS,"
                    +"VDPLO= :VDPLO,"
                    +"VDPVERSART= :VDPVERSART,"
                    +"VDPSPED= :VDPSPED,"
                    +"VDPVERSDAT= :VDPVERSDAT,"
                    +"VDPAVIS= :VDPAVIS,"
                    +"VDPRANG= :VDPRANG,"
                    +"VDPVERSADR= :VDPVERSADR,"
                    +"VDPPLZ= :VDPPLZ,"
                    +"VDPORT= :VDPORT,"
                    +"VDPAUFM= :VDPAUFM,"
                    +"VDPMG= :VDPMG,"
                    +"VDPFILLER= :VDPFILLER,"
                    +"VDPAENZ= :VDPAENZ,"
                    +"VDPAENB= :VDPAENB,"
                    +"VDPAEND= :VDPAEND,"
                    +"VDPNEUB= :VDPNEUB,"
                    +"VDPNEUD= :VDPNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("VDPMANDANT", OracleType.NVarChar).Value = dwData["VDPMANDANT"].ToString(); 
          or1.Parameters.Add("VDPAUFTYP", OracleType.NVarChar).Value = dwData["VDPAUFTYP"].ToString(); 
          or1.Parameters.Add("VDPAUFKRS", OracleType.NVarChar).Value = dwData["VDPAUFKRS"].ToString(); 
          or1.Parameters.Add("VDPAUFLFD", OracleType.NVarChar).Value = dwData["VDPAUFLFD"].ToString(); 
          or1.Parameters.Add("VDPAUFPOS", OracleType.NVarChar).Value = dwData["VDPAUFPOS"].ToString(); 
          or1.Parameters.Add("VDPPOS", OracleType.NVarChar).Value = dwData["VDPPOS"].ToString(); 
          or1.Parameters.Add("VDPLO", OracleType.NVarChar).Value = dwData["VDPLO"].ToString(); 
          or1.Parameters.Add("VDPVERSART", OracleType.NVarChar).Value = dwData["VDPVERSART"].ToString(); 
          or1.Parameters.Add("VDPSPED", OracleType.NVarChar).Value = dwData["VDPSPED"].ToString(); 
          or1.Parameters.Add("VDPVERSDAT", OracleType.NVarChar).Value = dwData["VDPVERSDAT"].ToString(); 
          or1.Parameters.Add("VDPAVIS", OracleType.NVarChar).Value = dwData["VDPAVIS"].ToString(); 
          or1.Parameters.Add("VDPRANG", OracleType.NVarChar).Value = dwData["VDPRANG"].ToString(); 
          or1.Parameters.Add("VDPVERSADR", OracleType.NVarChar).Value = dwData["VDPVERSADR"].ToString(); 
          or1.Parameters.Add("VDPPLZ", OracleType.NVarChar).Value = dwData["VDPPLZ"].ToString(); 
          or1.Parameters.Add("VDPORT", OracleType.NVarChar).Value = dwData["VDPORT"].ToString(); 
          or1.Parameters.Add("VDPAUFM", OracleType.NVarChar).Value = dwData["VDPAUFM"].ToString(); 
          or1.Parameters.Add("VDPMG", OracleType.NVarChar).Value = dwData["VDPMG"].ToString(); 
          or1.Parameters.Add("VDPFILLER", OracleType.NVarChar).Value = dwData["VDPFILLER"].ToString(); 
          or1.Parameters.Add("VDPAENZ", OracleType.NVarChar).Value = dwData["VDPAENZ"].ToString(); 
          or1.Parameters.Add("VDPAENB", OracleType.NVarChar).Value = dwData["VDPAENB"].ToString(); 
          or1.Parameters.Add("VDPAEND", OracleType.NVarChar).Value = dwData["VDPAEND"].ToString(); 
          or1.Parameters.Add("VDPNEUB", OracleType.NVarChar).Value = dwData["VDPNEUB"].ToString(); 
          or1.Parameters.Add("VDPNEUD", OracleType.NVarChar).Value = dwData["VDPNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
